package com.cn.web.controller.mc;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.K3bm;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.mc.K3FlXx;
import com.cn.bean.pojo.mc.K3FlXxDG;
import com.cn.config.config.Result;
import com.cn.service.server.mc.K3FlXxDGService;
import com.cn.service.server.mc.K3FlXxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/MC")
public class K3FlXxDGController {
    @Autowired(required = false)
    private K3FlXxDGService flXxDGService;
    @Autowired(required = false)
    private K3FlXxService flXxService;

    @RequestMapping("/add/K3/Flxx/DG")
    @ResponseBody
    public Result addK3FlXxDG(@RequestBody JSONObject jsonObject) {
        List<K3FlXxDG> k3FlXxDGS = flXxDGService.selectK3XxDG();
        List<K3FlXxDG> k3FlXxDGS1 = flXxService.selectK3FlXxAll();
        List<K3FlXxDG> k3NotInList1 = k3FlXxDGS.stream()
                .filter(kh -> k3FlXxDGS1.stream().noneMatch(kh1 -> Objects.equals(kh.getK3WlBm(), kh1.getK3WlBm())))
                .collect(Collectors.toList());
        boolean isOK = true;
        for (K3FlXxDG k3FlXxDG : k3NotInList1) {
            int i = flXxService.addK3FlXxDao(k3FlXxDG);
            if (i == 0) {
                isOK = false;
            }
        }
        if (!isOK){
            return Result.failure("500","更新失败");
        }
        return Result.success("200", "更新成功");
    }
    @RequestMapping("/get/K3/Flxx/DG")
    @ResponseBody
    public List<K3FlXxDG> getK3FlXxDG(@RequestBody JSONObject jsonObject) {
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String k3WlBm = jsonObject.getString("k3WlBm");
        String k3Mc = jsonObject.getString("k3Mc");
        String flMc = jsonObject.getString("flMc");
        k3WlBm = (k3WlBm != null) ? k3WlBm : "";
        k3Mc = (k3Mc != null) ? k3Mc : "";
        flMc = (flMc != null) ? flMc : "";
        List<K3FlXxDG> k3FlXx = flXxService.getK3FlXx(page, limit, k3WlBm, k3Mc, flMc);
        return k3FlXx;
    }
}
